<?php
/*
 *   This file is part of verbum.
 *
 *   poat is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   poat is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with poat.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * 	 Copyright (c) 2008 Sergio Gabriel Teves <gabriel.sgt at gmail.com>
 */

/*
1 Unknown method

    Returned if the server was asked to dispatch a method it didn't know about
2 Invalid return payload

    This error is actually generated by the client, not server, code, but signifies that a server returned something it couldn't understand. A more detailed error report is sometimes added onto the end of the phrase above.
3 Incorrect parameters

    This error is generated when the server has signature(s) defined for a method, and the parameters passed by the client do not match any of signatures.
4 Can't introspect: method unknown

    This error is generated by the builtin system.* methods when any kind of introspection is attempted on a method undefined by the server.
5 Didn't receive 200 OK from remote server

    This error is generated by the client when a remote server doesn't return HTTP/1.1 200 OK in response to a request. A more detailed error report is added onto the end of the phrase above.
6 No data received from server

    This error is generated by the client when a remote server returns HTTP/1.1 200 OK in response to a request, but no response body follows the HTTP headers.
7 No SSL support compiled in

    This error is generated by the client when trying to send a request with HTTPS and the CURL extension is not available to PHP.
8 CURL error

    This error is generated by the client when trying to send a request with HTTPS and the HTTPS communication fails.
9-14 multicall errors

    These errors are generated by the server when something fails inside a system.multicall request.
100- XML parse errors

    Returns 100 plus the XML parser error code for the fault that occurred. The faultString returned explains where the parse error was in the incoming XML stream.
 */
 
function searchLocal($param) {
	global $CURRENT_USER;
	global $loader;
	global $_log;
	
	$gl = new glossary;
	$gl->teamid = getTeamId($CURRENT_USER);
	return $loader->loadListBy($gl,$gl->find($param['id']));	
}

function searchOpen($param) {
	global $CURRENT_USER;
	global $loader;
	global $_log;
	
	require_once(ROOT_PATH."rpc/xmlrpc.inc");
	
	$t = new team;
	$t->id = getTeamId($CURRENT_USER);
	$r = $loader->get($t);

	$gl = new glossary;
	$gl->id="-1";
	$gl->english_term=$param['id'];
	$gl->related="";
	$gl->lastupdate="";
	$gl->userid="";
	$gl->translationlist=array();			

	$list = array();
	
	$_log->debug("Invoke open-tran.eu (".OPEN_TRAN_URL."-".OPEN_TRAN_METHOD.")");
	$m = new xmlrpcmsg(OPEN_TRAN_METHOD,array(new xmlrpcval($param['id']),new xmlrpcval($t->lang)));
	$c = new xmlrpc_client(OPEN_TRAN_URL);
	if (defined("PROXY_HOST") and PROXY_HOST!="") {
		$p = (defined("PROXY_PORT") and PROXY_PORT!="") ? PROXY_PORT : 80;
		$_log->debug("Using proxy ".PROXY_HOST.":$p");
		$c->setProxy(PROXY_HOST,(int) $p);	
	}
	$r = $c->send($m,(int)EXEC_TIMEOUT);	
	if (!$r->faultCode()) {
		$rres = $r->value()->me['array'];
		if (count($rres>0)) {
			$_log->debug("Suggested by open-tran.eu: ".count($rres));
			//$_log->debug($r->serialize());
			$gl->comments=String::format(s("Suggested by open-tran.eu: ({0})"),count($rres));
			$ar = array();
			foreach ($rres as $ob) {
				$text = htmlentities($ob->me['struct']['text']->me['string']);
				array_push($ar,$text);
			}
			$gl->translationlist=$ar;
		} else {
			$_log->debug("No suggestions available from open-tran.eu");
			$gl->comments=s("No suggestions available from open-tran.eu");
		}
	} else {
		$_log->debug("Service open-tran.eu not available. [".$r->faultCode()." | ".$r->faultString()."]");
		$gl->comments=s("The Service open-tran.eu is not available or is taking too long");
	}
	array_push($list,$gl);
	return $list;		
}

/* python version */
//function searchOpen($param) {
//	global $CURRENT_USER;
//	global $loader;
//	global $_log;
//	
//	$t = new team;
//	$t->id = getTeamId($CURRENT_USER);
//	$r = $loader->get($t);
//
//	$_log->debug("Call open-tran.eu");
//	$opensfile = ROOT_PATH."bash/opentran_rpc.py";
//	$rcpres=array();
//	$execret = 0;
//	$list = array();
//	
//	if ($r) {
//		@exec($opensfile." \"".$param['id']."\" ".$t->lang."",$rcpres,$execret);	
//	} else {
//		$execret = "-1";
//	}
//	
//	$gl = new glossary;
//	$gl->obj_array['id']="-1";
//	$gl->obj_array['english_term']=$param['id'];
//	$gl->obj_array['related']="";
//	$gl->obj_array['lastupdate']="";
//	$gl->obj_array['userid']="";
//	$gl->obj_array['translationlist']=array();			
//	if ($execret==0) {
//		if (count($rcpres)>0) {
//			$_log->debug("Suggested by open-tran.eu: ".count($rcpres));
//			$gl->obj_array['comments']=s("Suggested by open-tran.eu");
//			$ar = array();
//			$rcpresi = str_replace("'","\"",$rcpres);
//			foreach ($rcpresi as $job) {
//				$ob = json_decode($job);
//				if ($ob->text!=null) {
//					array_push($ar,$ob->text);	
//				}
//			}
//			$gl->obj_array['translationlist']=$ar;
//		} else {
//			$_log->debug("No suggestions available from open-tran.eu");
//			$gl->obj_array['comments']=s("No suggestions available from open-tran.eu");
//		}
//	} else {
//		$_log->debug("Service open-tran.eu not available [$execret]");
//		$gl->obj_array['comments']=s("Service open-tran.eu not available [$execret]");
//	}
//	array_push($list,$gl);
//	return $list;	
//}


?>
